import {
	baseUrl
} from "@/utils/config.js";
export function request(config) {
	//设置请求头
	config.header = {
		"content-type": "application/json",
	};
	//检查缓存中有没有token
	var token = uni.getStorageSync("userInfo").token;
	if (token) {
		config.header.TOKEN = token;
	} else {
		//不存在则跳转至登录页
		// uni.reLaunch({
		// 	url: "/pages/login/index",
		// });
	}
	config.url = baseUrl + config.url;
	let promise = new Promise(function(resolve, reject) {
		uni.request(config)
			.then((res) => {
				// console.log("接口", res);
				let data = res.data;
				if (data.code === 401) {
					uni.showToast({
						title: '登录已过期，请重新登录！',
						icon: 'none'
					})
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/login/login'
						})
					},600)
				} else {
					resolve(data);
				}
			})
			.catch((error) => {
				reject(error);
			});
	});
	return promise;
}